package hot100.logos51To60;

import java.util.ArrayList;
import java.util.List;

/**
 * @author logos
 * date 2025/7/15 12:53
 * @version v1.0
 * @Package hot100.logos51To60
 */
public class logos56 {

    /**
     * 56. 子集
     * https://leetcode.cn/problems/subsets/?envType=study-plan-v2&envId=top-100-liked
     */

    class Solution1 {
        public List<List<Integer>> subsets(int[] nums) {
            List<List<Integer>> ans = new ArrayList<>();
            solve(nums, 0, new int[nums.length], 0, ans);
            return ans;
        }

        private void solve(int[] nums, int i, int[] path, int size, List<List<Integer>> ans) {
            if (i == nums.length) {
                List<Integer> tmp = new ArrayList<>();
                for (int j = 0; j < size; j++) {
                    tmp.add(path[j]);
                }
                ans.add(tmp);
            } else {
                solve(nums, i + 1, path, size, ans);
                path[size++] = nums[i];
                solve(nums, i + 1, path, size, ans);
            }
        }
    }

    class Solution2 {
        public List<List<Integer>> subsets(int[] nums) {
            List<List<Integer>> ans = new ArrayList<>();
            solve(nums, 0, new ArrayList<>(), ans);
            return ans;
        }

        private void solve(int[] nums, int i, List<Integer> path, List<List<Integer>> ans) {
            if (i == nums.length) {
                List<Integer> tmp = new ArrayList<>();
                for (int j = 0; j < path.size(); j++) {
                    tmp.add(path.get(j));
                }
                ans.add(tmp);
            } else {
                solve(nums, i + 1, path, ans);
                path.add(nums[i]);
                solve(nums, i + 1, path, ans);
                path.remove(path.size() - 1);
            }
        }
    }
}
